<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/mi_project/css/swiper.min.css">
    <script src="/mi_project/js/swiper.js"></script>
    <script src="/mi_project/js/jquery.js"></script>
    <script src="/mi_project/js/jquery.cookie.js"></script>
    <script src="/mi_project/js/common.js"></script>
    <link rel="stylesheet" href="/mi_project/css/base.css">
    <link rel="stylesheet" href="/mi_project/css/index-top.css">
    <link rel="stylesheet" href="/mi_project/css/details.css">
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_2917027_4icbe1xja37.css">
    <script src="/mi_project/layui/layui.js"></script>
</head>
<body>
    <div class="header-1">
    </div>
    <div class="header-2">
    </div>
    <div class="header-3">
        <h2 class="w"></h2>
    </div>
    <div class="detailBox">
        <div class="w">
            <div class="detailImg">
                <div class="swiper-container">
                    <div class="swiper-wrapper">
                    </div>
                    <!-- Add Pagination -->
                    <div class="swiper-pagination swiper-pagination-white"></div>
                    <!-- Add Arrows -->
                    <div class="swiper-button-next swiper-button-white"></div>
                    <div class="swiper-button-prev swiper-button-white"></div>
                </div>
            </div>
            <div class="detailtext">
                <h2 class="goodsName">asd</h2>
                <h3 class="goodsBody">asdasd</h3>
                <span>小米自营</span>
                <div><span class="marketPrice">123</span><del class="shopPrice">123</del></div>
                <span>选择数量</span>
                <div>
                    <input type="button" class="less" value="-"><input type="text" class="amount" value="1"><input
                        type="button" class="add" value="+">
                </div>
                <div>
                    <li><span class="goodsName">123</span><span class="shopPrice">123</span></li>
                    <div class="amountPrice">123</div>
                </div>
                <div class="addcart">加入购物车</div>
            </div>
        </div>
    </div>
</body>

</html>
<script>
    $.ajax({
        url: urls + '/goods/detail',
        data: { goodsId: getUrlParam('id') },
        success: function (backData) {
            $('.header-3 h2').html(backData.data.goodsName);
            $('.goodsName').html(backData.data.goodsName);
            $('.goodsBody').html(backData.data.goodsBody);
            $('.marketPrice').html(backData.data.marketPrice);
            $('.shopPrice').html(backData.data.shopPrice);
            $('.add').click(function () {
                $('.amount').val($('.amount').val() * 1 + 1);
                $('.amountPrice').text(`总计：${$('.amount').val() * backData.data.marketPrice}元`);
            })
            $('.less').click(function () {
                if ($('.amount').val() * 1 <= 1) {
                    alert("不可以哦")
                } else {
                    $('.amount').val($('.amount').val() * 1 - 1);
                    $('.amountPrice').text(`总计：${$('.amount').val() * backData.data.marketPrice}元`);
                }
            })
            $('.amount').on('input propertychange',function () {
                if ($('.amount').val() * 1 < 1) {
                    alert("不可以哦");
                    $('.amount').val(1);
                } else {
                    $('.amountPrice').text(`总计：${$('.amount').val() * backData.data.marketPrice}元`);
                }
            })
            $('.amountPrice').text(`总计：${$('.amount').val() * backData.data.marketPrice}元`);
            if (Object.prototype.toString.call(backData.data.goodsImg) == '[object Array]' && backData.data.goodsImg.length > 1) {
                for (key in backData.data.goodsImg) {
                    let dom = $(`<div class="swiper-slide" style="background-image:url(${backData.data.goodsImg[key].goodsImg})">`);
                    $('.swiper-wrapper').append(dom);
                }
                let swiper = new Swiper('.swiper-container', {
                    spaceBetween: 30,
                    effect: 'fade',
                    pagination: {
                        el: '.swiper-pagination',
                        clickable: true,
                    },
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                });
            } else {
                $('.detailImg').prop('style', `background-image:url(${backData.data.goodsThumb})`);
            }

        }
    })

    $('.addcart').click(function(){
        if($.cookie('user')){
            $.ajax({
            url:urls+"/cart/add",
            data:{
                userId:$.cookie('user').id,
                goodsId:getUrlParam('id'),
                goodsCount:$('.amount').val()
            },
            success:function(backData){
                if(backData.code==0){
                    layer.msg('添加成功')
                }else{
                    layer.msg('添加失败')
                }
            }
        })
        }else{
            let userDataT=localStorage.getItem('userData')?JSON.parse(localStorage.getItem('userData')):{};
            if(userDataT[getUrlParam('id')]){
                userDataT[getUrlParam('id')] +=$('.amount').val()*1;
            }else{
                userDataT[getUrlParam('id')]=$('.amount').val()*1;
            }
            localStorage.setItem('userData',JSON.stringify(userDataT));
        }
    })
    
</script>